<?php

namespace App\Admin;

use App\Logic\AdminUser as LogicAdminUser;
use App\Models\AdminUser as ModelsAdminUser;
use App\Utils\Code;
use Illuminate\Http\Request;

class AdminUser extends BaseController
{

    function __construct()
    {
        parent::__construct();
        $this -> model = new ModelsAdminUser;
        $this -> logic = new LogicAdminUser;
    }

    /**
     * 管理员列表
     */
    public function userList(Request $request)
    {
        $param = $request->input();
        $result = $this->model->list($param);
        if ($result) return renderData($result);
        return renderError(Code::FAILED_TO_FIND_DATA);
    }

    /**
     * 添加管理员
     */
    public function addUser(Request $request)
    {
        $rules  = [
            'account'       => 'required|unique:admin_user',
            'password'      => 'required',
            'user_name'     => 'nullable',
            'head_pic'      => 'nullable',
            'role_id'       => 'nullable',
            'sex'           => 'nullable',
            'status'        => 'nullable'
        ];
        if ($error = $this->formValidate($request, $rules)) return renderError(Code::PARAM_IS_ERROR, $error);
        $data = $this -> validate($request, $rules);
        $data['password'] = password_hash($data['password'], PASSWORD_BCRYPT);
        $data['create_time'] = time();
        $result = $this -> model -> insert($data);
        if ($result) return renderSuccess('添加成功');
        return renderError(Code::ADD_FAILED);

    }

    /**
     * 修改管理员
     */
    public function updateUser(Request $request)
    {
        $rules = [
            'id'            => 'required',
            'account'       => 'required|unique:admin_user,account,'.$request->input('id', 0).',id',
            'user_name'     => 'nullable',
            'head_pic'      => 'nullable',
            'role_id'       => 'nullable',
            'sex'           => 'nullable',
            'status'        => 'nullable'
        ];
        if ($error = $this->formValidate($request, $rules)) return renderError(Code::PARAM_IS_ERROR, $error);
        $data = $this -> validate($request, $rules);
        $id = $data['id'];
        unset($data['id']);
        $result = $this -> model -> updateInfo($data, ['id' => $id]);
        if ($result) return renderSuccess('修改成功');
        return renderError(Code::UPDATE_FAILED);

    }

    /**
     * 删除管理员
     */
    public function deleteUser(Request $request)
    {
        $rules = ['id'  => 'required'];
        if ($error = $this->formValidate($request, $rules)) return renderError(Code::PARAM_IS_ERROR, $error);
        $id = $request -> input('id');
        $result = $this -> model -> where(['id' => $id]) -> delete();
        if ($result) return renderSuccess('删除成功');
        return renderError(Code::DELETE_FAILED);
    }

    /**
     * 后台登录
     */
    public function login(Request $request)
    {
        $rules = [
            'account'       => 'required',
            'password'      => 'required'
        ];
        if ($error = $this -> formValidate($request, $rules, $data)) return renderError(Code::PARAM_IS_ERROR, $error);
        $result = $this -> logic -> login($data['account'], $data['password']);
        if (!$result) return renderError(Code::AUTH_IS_ERROR);
        return renderData($result);
    }

    /**
     * 后台登出
     */
    public function logout(Request $request)
    {
        if (!$token = $request -> header('token')) return renderError(Code::TOKEN_IS_EMPTY);
        $this -> logic -> logout($token);
        return renderSuccess('登出成功');
    }

}